//
//  YMAccountAuthorizationListCell.m
//  YMMedical
//
//  Created by y s on 2023/7/26.
//

#import "YMAccountAuthorizationListCell.h"

@interface YMAccountAuthorizationListCell ()

@property (nonatomic, strong) UIView *vBg;
@property (nonatomic, strong) UILabel *lblAccount;
@property (nonatomic, strong) UIButton *btnBinding;

@end

@implementation YMAccountAuthorizationListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark - UI methods
- (void)setUI{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = [UIColor clearColor];
    self.backgroundColor = [UIColor clearColor];
    
    [self.contentView addSubview:self.vBg];
    [self.vBg addSubview:self.lblAccount];
    [self.vBg addSubview:self.btnBinding];

    [self.vBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.top.mas_equalTo(0);
    }];
    [self.lblAccount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.btnBinding.mas_left).offset([SFitUtil fit:-5]);
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo([SFitUtil fit:15]);
    }];
    [self.btnBinding mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo([SFitUtil fitSize:55 height:27]);
    }];
}


#pragma mark - public methods
+ (CGFloat)getCellHeight{
    return [SFitUtil fit:58];
}
- (void)setShowBtn:(BOOL)showBtn{
    _showBtn = showBtn;
    self.btnBinding.hidden = !showBtn;
}
- (void)setDict:(NSDictionary *)dict{
    _dict = dict;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *date = [dateFormatter dateFromString:[SDataUtil stringOf:dict[@"created_at"] valid:@""]];
    dateFormatter.dateFormat = @"yyyy/MM/dd";
    NSString *dateStr = [dateFormatter stringFromDate:date];
    NSString *phoneStr = [SDataUtil stringOf:dict[@"xoroodxlsn4riasa"] valid:@""];
    if (phoneStr != nil && phoneStr.length > 10 &&
        ![phoneStr isEqualToString:[YMMyModel shared].userPhone]){
        phoneStr = [phoneStr stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
    }
    self.lblAccount.text = [NSString stringWithFormat:@"%@（%@%@）",phoneStr,dateStr,KLanguage(@"bind_txt")];
    
    if ([phoneStr isEqualToString:[YMMyModel shared].userPhone]){
        self.btnBinding.hidden = NO;
    }else{
        self.btnBinding.hidden = YES;
    }
}


- (void)bindingClickAction:(id)sender{
    if (self.deleteUserBlock != nil){
        self.deleteUserBlock();
    }
}

#pragma mark - setter and getter
- (UIView *)vBg{
    if (_vBg == nil){
        _vBg = [[UIView alloc] init];
        _vBg.backgroundColor = STheme.D9;
        _vBg.layer.cornerRadius = [SFitUtil fit:8];
        _vBg.clipsToBounds = YES;
    }
    return _vBg;
}
- (UILabel *)lblAccount{
    if (_lblAccount == nil){
        _lblAccount = [[UILabel alloc] init];
        _lblAccount.textColor = STheme.D1;
        _lblAccount.font = [SFitUtil fitFont:14];
    }
    return _lblAccount;
}
- (UIButton *)btnBinding{
    if (_btnBinding == nil){
        _btnBinding = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnBinding.layer.borderColor = [SUIUtil colorOf:0xCCCCCC].CGColor;
        _btnBinding.layer.borderWidth = [SFitUtil fitLine];
        _btnBinding.clipsToBounds = YES;
        _btnBinding.layer.cornerRadius = [SFitUtil fit:4];
        [_btnBinding setTitle:KLanguage(@"unbind_txt") forState:UIControlStateNormal];
        [_btnBinding setTitleColor:STheme.D12 forState:UIControlStateNormal];
        _btnBinding.titleLabel.font = [SFitUtil fitBoldFont:14];
        _btnBinding.titleLabel.adjustsFontSizeToFitWidth = YES;
        _btnBinding.titleLabel.minimumScaleFactor = 0.6;
        [_btnBinding addTarget:self action:@selector(bindingClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnBinding;
}

@end
